package com.muki.succ.web.component;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class ThemePublisher {

	@Autowired private JmsMessagingTemplate jmsMessagingTemplate;
	
	@Resource(name="theme") private Queue theme;
	
	@Resource(name="tasks") private Queue tasks;
	
	@Resource(name="init-url") private Queue queue;
	
	@Resource(name="urls") private Queue urls;
	
	@Resource(name="content-seen") private Queue content;
	
	/**
	 * 发送消息主题
	 * @param topic
	 * @param num
	 */
	public void sendTopic(String topic,int num) {
		Map<String,String> params = new HashMap<String,String>();
		params.put("topic", topic);
		params.put("num", String.valueOf(num));
		jmsMessagingTemplate.convertAndSend(this.theme,params);
	}
	
	public void pubTask(String topic,String url) {
		pubTask(topic,url,1000,-1);
	}
	
	public void pubTask(String topic,String url,long delay) {
		pubTask(topic,url,delay,-1);
	}
	
	public void pubTask(String topic,String url,int time) {
		pubTask(topic,url,1000,time);
	}
	
	public void pubTask(String topic,String url,long delay,int time) {
		Map<String,String> params = new HashMap<String,String>();
		params.put("topic", topic);
		params.put("init-url", url);
		params.put("delay", String.valueOf(delay));
		params.put("time",String.valueOf(time));
		jmsMessagingTemplate.convertAndSend(this.tasks,params);
	} 
	
	/**
	 * 发送启动url
	 * @param url
	 */
	public void sendInitUrl(String[] url) {
		jmsMessagingTemplate.convertAndSend(this.queue, url);
	}
	
	/**
	 * 解析url时
	 * 获取到url，通过urls通道发送
	 * @param url
	 */
	public void sendUrl(String topic,String url) {
		jmsMessagingTemplate.convertAndSend(this.urls,topic+" "+url);
	}
}
